<?php

/*
 * File tellafriend.php for project Rijlesplanner.nl
 * Created on 30 nov 2007 at 21:09:53 by Ivar Pruijn
 */
class tellafriend extends modules {
	// Properties:
	protected $name = "tellafriend";
	protected $title = "Tell-a-friend";
	protected $subtitle = "";
	protected $moduleOptions = array ();
	private $errors = array ();

	// Methods:

	/**
	 * void __construct ()
	 * This is the constructor method for this class.
	 */
	public function __construct() {
		parent :: __construct();
		$this->logger = parent :: getLogger();
		$this->user = parent :: getUser();
		$this->template = parent :: getTemplate();
	}

	/**
	 * Object process()
	 * 
	 * Processes the template and current module
	 * 
	 * @param Smarty template The Smarty template to process (sets variables etc.)
	 * @return Smarty The template that has been processed
	 */
	function process(& $template = null) {
		$this->template = $template;
		if (!empty ($_POST)) {
			switch ($_POST['action']) {
				case "sendtellafriend" :
					if ($this->auth->accessTo($this->name,"",$_POST['action'])) {
						// send the message
						if($this->sendMessage()) {
							$this->template->assign('moduleTemplate', 'modules/tellafriend/tellafriend.tpl');
						} else {
							$this->template->assign('moduleTemplate', 'modules/tellafriend/tellafriend.tpl');
						}
					} else {
						$this->actionAccessDenied();
					}
					break;
			}
		} else {
			$this->template->assign('moduleTemplate', 'modules/tellafriend/tellafriend.tpl');
		}
	}

	/**
	 * boolean sendMessage()
	 * 
	 * Send the tell-a-friend message
	 * Sets an error array in the template in case of errors.
	 * 
	 * @return boolean whether the mailing was sucessful
	 */
	function sendMessage() {
		$messageSent = array('result'=>false);
		$this->logger->log("Sending message");
		$this->errors = array ();
		
		if (!isset ($_POST['friend_name']) || empty ($_POST['friend_name'])) {
			$this->addError('Vul s.v.p. de naam van je vriend(in) in', 9);
		}
		if (!isset ($_POST['friend_email']) || empty ($_POST['friend_email'])) {
			$this->addError('Vul s.v.p. het e-mailadres van je vriend(in) in', 10);
		} else {
			if (isset ($_POST['friend_email']) && !validate_email($_POST['friend_email']) ) {
				$this->addError('Het e-mailadres van je vriend is niet correct', 15);
			}
		}
		if (!isset ($_POST['your_name']) || empty ($_POST['your_name'])) {
			$this->addError('Vul s.v.p. jouw naam in', 11);
		}
		if (!isset ($_POST['your_email']) || empty ($_POST['your_email'])) {
			$this->addError('Vul s.v.p. jouw e-mailadres in', 12);
		} else {
			if (isset ($_POST['your_email']) && !validate_email($_POST['your_email']) ) {
				$this->addError('Jouw e-mailadres is niet correct', 16);
			}
		}
		if (!isset ($_POST['your_message']) || empty ($_POST['your_message'])) {
			$this->addError('Vul s.v.p. een bericht in', 13);
		}
		if (isset ($_POST['your_message']) && strlen($_POST['your_message']) > $this->config['tellafriend_message_maxcharacters']) {
			$this->addError('Het bericht mag maximaal '. $this->config['tellafriend_message_maxcharacters'].' tekens bevatten', 14);
		}

		if (count($this->errors) == 0) {
			try {
				$this->logger->log("Tell a friend webservice call" . $_POST['your_name']);
				if($this->user->getUserType() == 0) {
					// anoniem
					$rijschoolId = 0;
				} else {
					$rijschoolId = $this->user->getRijschoolId();
				}
				$result = $this->wsclient->WS_Rijlesplanner_TellAFriend(array (
					'SenderName' => $_POST['your_name'],
					'SenderEmail' => $_POST['your_email'],
					'RecipientName' => $_POST['friend_name'],
					'RecipientEmail' => $_POST['friend_email'],
					'Content' => $_POST['your_message']
				));
				
				$this->logger->log("Result tell-a-friend: ".print_r($result,true),PEAR_LOG_DEBUG);
				$sendResult = $result->Success;
				$this->template->assign('sendResult', $sendResult);
				$this->template->assign('emailadres', $_POST['friend_email']);
			} catch (SoapFault $e) {
				$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_DEBUG);
				$this->logger->log("WS Client".print_r($this->wsclient,true),PEAR_LOG_DEBUG);
				switch ($e->faultcode) {
					default :
						$errorcode = 61;
						$errormessage = 'Onbekende fout bij opslaan Tell-a-friend statistieken';
						break;
				}
				$this->addError($errormessage,$errorcode);
			}
		}

		$this->template->assign('errors', $this->errors);
		$this->template->assign('messageSent', $messageSent);
	}

	public function addError($message, $code) {
		$this->errors[] = array (
			'code' => $code,
			'message' => $message
		);
		$this->setSubtitle($message);
	}

	public function getTitle() {
		return $this->title;
	}

	public function getSubTitle() {
		if (!empty ($_GET['option'])) {
			$subtitle = "";
			foreach ($this->moduleOptions AS $mOpt) {
				if ($mOpt['name'] == $_GET['option']) {
					$subtitle = $mOpt['title'];
				}
			}
			return $subtitle;
		} else {
			return $this->subtitle;
		}
	}

	public function setSubTitle($newVal = "") {
		if (!empty ($newVal)) {
			$this->subtitle = $newVal;
		}
	}

	/*
	 * string __toString()
	 * 
	 * Returns a String representation of this class.
	 * 
	 * @return string A representation of this class
	 */
	public function __toString() {
		return $this->title;
	}
}
?>
